import env from '../config/env'
export default {
	getUid : function() {
		var uid = window.sessionStorage.getItem("uid");
		if(uid){
			return uid;
	   	}
		function S4() {
	       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	    }
		uid = (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
		window.sessionStorage.setItem("uid",uid);
	    return uid;
	},
	getApiPrefix:function(){
		return env.gwApiPrefix;
	},
	getFtpPrefix:function(){
		return env.sftpPathPrefix;
	},
	getWeixinPrefix:function(){
		return env.weixinPrefix;
	},
	fixTicketPara: function(url) {
		var ticket = this.getTicket();
		if(ticket) {
			if(url.indexOf("?") >= 0) {
				url = url + "&ticket=" + ticket;
			} else {
				url = url + "?ticket=" + ticket;
			}
		}
		return url;
	},
	doPost: function(url, data, success, error, async) {
		if(async != false) {
			async = true;
		}
		url = this.fixTicketPara(url);
		$.ajax({
			crossDomain: true,
			url: url,
			type: "POST",
			data: JSON.stringify(data),
			dataType: "JSON",
			async: async,
			contentType: "application/json",
			success: success,
			error: error
		});
	},
	doFileUpload: function(url, data, success, error, async) {
		if(async != false) {
			async = true;
		}
		//url = this.fixTicketPara(url);
		$.ajax({
			crossDomain: true,
			url: url,
			type: "POST",
			data: JSON.stringify(data),
			dataType: "JSON",
			async: async,
			contentType: "multipart/form-data",
			success: success,
			error: error
		});
	},

	doGet: function(url, success, error, async) {
		if(async != false) {
			async = true;
		};
		url = this.fixTicketPara(url);
		$.ajax({
			crossDomain: true,
			type: "GET",
			url: url,
			data: null,
			dataType: "JSON",
			async: async,
			success: success,
			error: error
		});
	},
	isLogin: function() {
		var rst = "";
		var url = this.getApiPrefix() + "/api/auth/isLogin";
		this.doGet(url, function(data) {
			rst = data;
		}, null, false);

		return rst == "true";
	},
	logout: function(success) {
		var url = this.getApiPrefix() + "/api/auth/logout";
		this.doGet(url, success);
		window.localStorage.removeItem("ticket");
	},
	setTicket: function(ticket) {
		//$.cookie("ticket",ticket);
		window.localStorage.setItem("ticket", ticket);
	},
	getTicket: function() {
		//return $.cookie("ticket");
		return window.localStorage.getItem("ticket");
	},
	setLoginUser:function(user){
		window.localStorage.setItem("loginuser", JSON.stringify(user));
	},
	getLoginUser: function() {
		var userStr = window.localStorage.getItem("loginuser");
		return JSON.parse(userStr);
	},
	setLoginType:function(loginType){
		window.localStorage.setItem("loginType", loginType);
	},
	getLoginType: function() {
		return window.localStorage.getItem("loginType");
	},
	getUploadFilePath:function(path){
	    return this.getFtpPrefix()+"/"+path;
	},
	goServiceUrl:function(service) {
		if(!service) {
			if(!service){
				var service = this.getZhisuPortalPrefix()+"/#/center/person/project";
				var user = this.getLoginUser();
				if(user&&user.type=='COMPANY'){
					service = this.getZhisuPortalPrefix()+"/#/center/company/home";
				}
			}
		} else {
			service = decodeURIComponent(service);
		}
		var index = service.indexOf("ticket=");
		if(index!=-1){
			service = service.substr(0,index-1);
		}
		if(service.indexOf("?") >= 0) {
			service += "&ticket=" + this.getTicket();
		} else {
			service += "?ticket=" + this.getTicket();
		}
		location.href = service;

	}
}